<template>
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" v-dialogDrag :visible.sync="visible">
			<el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading"
				:class="method==='view'?'readonly':''" :disabled="method==='view'" label-width="120px">
				<el-row :gutter="15">
					<el-col :span="12">
						<el-form-item label="税务名称" prop="taxname" :rules="[]">
							<el-select v-model="inputForm.taxname" placeholder="税务名称" style="width: 100%" >
                <el-option v-for="item in taxnameList" :key="item.id" :label="item.taxName" :value="item.id" ></el-option>
              </el-select>
						</el-form-item>
					</el-col>
          <el-col :span="12">
						<el-form-item label="部门" prop="department" :rules="[]">
							<el-select v-model="inputForm.department" placeholder="部门" style="width: 100%" >
                <el-option v-for="item in departmentList" :key="item.id" :label="item.name" :value="item.id" ></el-option>
              </el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="金额" prop="money" :rules="[]">
							<el-input v-model="inputForm.money" placeholder="请填写金额"></el-input>
						</el-form-item>
					</el-col>
          <el-col :span="12">
						<el-form-item label="备注" prop="remarks" :rules="[]">
							<el-input v-model="inputForm.remarks" placeholder="请填写备注" type="textarea"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button size="small" @click="visible = false">关闭</el-button>
				<el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定
				</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import SelectTree from '@/components/treeSelect/treeSelect.vue'
	import UserSelect from '@/components/userSelect'
	export default {
		data() {
			return {
				title: '',
				method: '',
				visible: false,
				loading: false,
        taxnameList:[],
        departmentList:[],
				inputForm: {
					id: '',
					department: '',
          company:this.$store.state.user.companyId,
					user:this.$store.state.user.id,
					money: '',
					remarks: '',
          taxname:''
				}
			}
		},
		components: {
			SelectTree,
			UserSelect
		},
		methods: {
			init(method, id) {
        this.getTaxNameList();
        this.getDepartmentList();
				this.method = method
				this.inputForm.id = id
				if (method === 'add') {
					this.title = `新建部门税务报表弹框`
				} else if (method === 'edit') {
					this.title = '修改部门税务报表弹框'
				} else if (method === 'view') {
					this.title = '查看部门税务报表弹框'
				}
				this.visible = true
				this.loading = false
				this.$nextTick(() => {
					this.$refs.inputForm.resetFields()
					if (method === 'edit' || method === 'view') { // 修改或者查看
						this.loading = true
						this.$http({
							url: `/rf/taxdepartment/taxDepartment/queryById?id=${this.inputForm.id}`,
							method: 'get'
						}).then(({
							data
						}) => {
							this.inputForm = this.recover(this.inputForm, data.taxDepartment)
							this.loading = false
						})
					}
				})
			},
			// 表单提交
			doSubmit() {
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true
						this.$http({
							url: `/rf/tax/saveTax`,
							method: 'post',
							data: this.inputForm
						}).then(({
							data
						}) => {
							this.loading = false
							if (data && data.success) {
								this.visible = false
								this.$message.success(data.msg)
								this.$emit('refreshDataList')
							}
						})
					}
				})
			},
      getDepartmentList(){
        this.$http({
          url: "/sys/office/getCompanyByOffice",
          method: "get",
          params: {
            company: this.$store.state.user.companyId
          },
          }).then(({ data }) => {
            if (data && data.success) {
              console.log("部门1",data);
              this.departmentList=data.List
            }
        });
      },
      getTaxNameList(){
         this.$http({
          url: "/rf/taxaffairs/taxAffairs/list",
          method: "get",
          params: {
            company: this.$store.state.user.companyId
          },
          }).then(({ data }) => {
            if (data && data.success) {
              console.log("税务名称1",data);
             this.taxnameList=data.page.list;
            }
        });
      },
		}
	}
</script>
